﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
using Expenses.ExpenseService;
using System.Collections.ObjectModel;
using System.Reflection;
using Windows.UI.Xaml;

namespace Expenses.ViewModels
{
    public class ExpenseItemViewModel : ViewModelBase
    {
        #region Properties

        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private int reportNumber = 0;
        public int ReportNumber
        {
            get
            { return reportNumber; }

            set
            {
                if (reportNumber == value)
                { return; }

                reportNumber = value;
                isDirty = true;
                RaisePropertyChanged("ReportNumber");
            }
        }

        private int employeeId = 0;
        public int EmployeeId
        {
            get
            { return employeeId; }

            set
            {
                if (employeeId == value)
                { return; }

                employeeId = value;
                isDirty = true;
                RaisePropertyChanged("EmployeeId");
            }
        }

        private int chargeId = 0;
        public int ChargeId
        {
            get
            { return chargeId; }

            set
            {
                if (chargeId == value)
                { return; }

                chargeId = value;
                isDirty = true;
                RaisePropertyChanged("ChargeId");
            }
        }

        private DateTime expenseDate = DateTime.Today;
        public DateTime ExpenseDate
        {
            get
            {
                return expenseDate;
            }

            set
            {
                if (expenseDate == value)
                { return; }

                expenseDate = value;
                isDirty = true;
                RaisePropertyChanged("ExpenseDate");
            }
        }

        private int daysOld = 0;
        public int DaysOld
        {
            get
            {
                return daysOld;
            }

            set
            {
                if (daysOld == value)
                { return; }

                daysOld = value;
                RaisePropertyChanged("DaysOld");
            }
        }

        private string merchant = string.Empty;
        public string Merchant
        {
            get
            { return merchant; }

            set
            {
                if (merchant == value)
                { return; }

                merchant = value;
                isDirty = true;
                RaisePropertyChanged("Merchant");
            }
        }

        private string location = string.Empty;
        public string Location
        {
            get
            { return location; }

            set
            {
                if (location == value)
                { return; }

                location = value;
                isDirty = true;
                RaisePropertyChanged("Location");
            }
        }

        private decimal billedAmount = 0M;
        public decimal BilledAmount
        {
            get
            { return billedAmount; }

            set
            {
                if (billedAmount == value)
                { return; }

                billedAmount = value;
                isDirty = true;
                RaisePropertyChanged("BilledAmount");
            }
        }

        private decimal transactionAmount = 0M;
        public decimal TransactionAmount
        {
            get
            { return transactionAmount; }

            set
            {
                if (transactionAmount == value)
                { return; }

                isDirty = true;
                transactionAmount = value;
                RaisePropertyChanged("TransactionAmount");
            }
        }

        private string description = string.Empty;
        public string Description
        {
            get
            { return description; }

            set
            {
                if (description == value)
                { return; }

                description = value;
                isDirty = true;
                RaisePropertyChanged("Description");
            }
        }

        private int category = 0;
        public int Category
        {
            get
            { return category; }

            set
            {
                if (category == value)
                { return; }

                category = value;
                isDirty = true;
                RaisePropertyChanged("Category");
            }
        }

        private List<string> categories;
        public List<string> Categories
        {
            get
            { return categories; }

            set
            {
                if (categories == value)
                { return; }

                categories = value;
                RaisePropertyChanged("Categories");
            }
        }

        private List<string> otherCategories;
        public List<string> OtherCategories
        {
            get
            { return otherCategories; }

            set
            {
                if (otherCategories == value)
                { return; }

                otherCategories = value;
                RaisePropertyChanged("OtherCategories");
            }
        }

        private int accountNumber = 0;
        public int AccountNumber
        {
            get
            { return accountNumber; }

            set
            {
                if (accountNumber == value)
                { return; }

                accountNumber = value;
                isDirty = true;
                RaisePropertyChanged("AccountNumber");
            }
        }

        private int costCenter = 0;
        public int CostCenter
        {
            get
            { return costCenter; }

            set
            {
                if (costCenter == value)
                { return; }

                costCenter = value;
                isDirty = true;
                RaisePropertyChanged("CostCenter");
            }
        }

        private bool receiptRequired = false;
        public bool ReceiptRequired
        {
            get
            { return receiptRequired; }

            set
            {
                if (receiptRequired == value)
                { return; }

                receiptRequired = value;
                RaisePropertyChanged("ReceiptRequired");
            }
        }

        private string notes = string.Empty;
        public string Notes
        {
            get
            {
                return notes;
            }
            set
            {
                if (notes == value)
                { return; }

                notes = value;
                isDirty = true;
                RaisePropertyChanged("Notes");
            }
        }

        private int expenseType = 1;
        public int ExpenseType
        {
            get
            { return expenseType; }

            set
            {
                if (expenseType == value)
                { return; }

                expenseType = value;
                RaisePropertyChanged("ExpenseType");
            }
        }

        private string imagePath = string.Empty;
        public string ImagePath
        {
            get
            {
                return imagePath;
            }
            set
            {
                if (imagePath == value)
                { return; }

                imagePath = value;
                isDirty = true;
                RaisePropertyChanged("ImagePath");
            }
        }

        private ImageSource image = null;
        public ImageSource Image
        {
            get
            {
                return this.image;
            }

            set
            {
                if (image == value)
                { return; }

                image = value;
                isDirty = true;
                RaisePropertyChanged("Image");
            }
        }

        private bool isDirty = false;
        public bool IsDirty
        {
            get
            {
                return isDirty;
            }

            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }

        #endregion "Properties"

        public async Task<string> SaveExpenseItem(ExpenseItemViewModel _expenseItem)
        {
            // Convert expense item to ExpenseItem 
            var expenseItem = new ExpenseItem
            {
                Id = _expenseItem.Id,
                ReportNumber=_expenseItem.ReportNumber,
                AccountNumber = Convert.ToInt32(_expenseItem.AccountNumber),
                BilledAmount = _expenseItem.BilledAmount,
                Category = Convert.ToInt32(_expenseItem.Category),
                Description = _expenseItem.Description,
                EmployeeId = _expenseItem.EmployeeId,
                ExpenseDate = _expenseItem.ExpenseDate,
                Location = _expenseItem.Location,
                Merchant = _expenseItem.Merchant,
                ReceiptRequired = _expenseItem.ReceiptRequired,
                TransactionAmount = _expenseItem.TransactionAmount,
                Notes = _expenseItem.Notes,
                ExpenseType=_expenseItem.ExpenseType
            };

            var expensesService = new ExpenseServiceClient();

            string result = await expensesService.SaveExpenseItemAsync(
                expenseItem.Id, expenseItem);
            return result;
        }

    }
}
